Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SD-22]Updated hero image theme according header style #516

Open
wants to merge 27 commits into
base: develop
Choose a base branch
from

Conversation

yeniatencio
Copy link
Contributor

@yeniatencio yeniatencio commented Oct 1, 2024

Jira

https://digital-vic.atlassian.net/browse/SD-22
BE testing link: https://nginx-php.pr-1711.content-vic.sdp4.sdp.vic.gov.au/

Problem/Motivation

We want to have these scenarios for customised header:

  1. Corner graphics - Light + Dark (Both options available and by default Light should be selected),
  2. Full background image - Dark only (Only one option should be set)
  3. CTA - Light only (Only one option should be set).
  4. Default appearance - Light only (Only one option should be set)

Fix

  1. Set field field_landing_page_hero_theme to mandatory.
  2. Remove a patch for drupal/jsonapi_extras as new released was done and patch is not needed anymore.
  3. Fix typo in label : Here image theme.
  4. Updated behat test.
  5. Updated tide publication module to make it work in publication and publication page content type.

Related PRs

Screenshots

Screenshot 2024-10-03 at 10 01 49 am Screenshot 2024-10-03 at 10 01 36 am Screenshot 2024-10-03 at 10 01 25 am Screenshot 2024-10-03 at 10 01 12 am

TODO

Copy link
Contributor

@MdNadimHossain MdNadimHossain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
},
};
})(Drupal);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not want use js if it is can be done without it. Read the SD, I have mentioned about conditional logic. You will find other examples in landing page where we have used conditional logic states. Take example from there and try to work it out using states instead of adding this JS please.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MdNadimHossain agreed. I've already using conditional logic for enable/disable the field_landing_page_hero_theme as per the requirements. However, one of the requirements is to set the default value of one field based on the value of another field and the only Drupal States that can be applied to a form field element are:

  • enabled
  • disabled
  • required
  • optional
  • visible
  • invisible
  • checked
  • unchecked
  • expanded
  • collapsed

As you can notice the default value can't be changed using states. Also, all the examples in landing page module is for changing one of the states in the list above but a default value.
Therefore, that's why I ended up writing JS code for it. Hope it makes sense now.

@yeniatencio yeniatencio changed the title [SD-22]added logic for landing page [SD-22]Updated hero image theme according header style Oct 8, 2024
Copy link
Contributor

@MdNadimHossain MdNadimHossain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yeniatencio I have suggested some suggestions. Please have a look into the revised version and it should work in the same way -

(function (Drupal) {
  "use strict";

  Drupal.behaviors.heroImageTheme = {
    attach: function (context, settings) {
      const imageTheme = context.querySelector(
        'select[data-drupal-selector^="edit-field-landing-page-hero-theme"]'
      );

      if (imageTheme !== null) {
        setImageTheme();

        const headerStyles = context.querySelectorAll(
          'input[data-drupal-selector^="edit-header-style-options"]'
        );

        if (headerStyles.length > 0) {
          headerStyles.forEach((style) => {
            style.addEventListener("change", setImageTheme);
          });
        }
      }

      function setImageTheme() {
        const selectedHeaderStyle = document.querySelector(
          'input[data-drupal-selector^="edit-header-style-options"]:checked'
        );

        if (!selectedHeaderStyle) {
          return; // Exit if no style option is selected.
        }

        let defaultHeaderStyle = selectedHeaderStyle.value;
        imageTheme.value = defaultHeaderStyle === "fullwidth" ? "dark" : "light";
      }
    },
  };
})(Drupal);


Drupal.behaviors.heroImageTheme = {
attach: function (context, settings) {
const imageTheme = document.querySelector(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use context to ensure that the behavior only attaches to elements in the current page context.

'input[data-drupal-selector^="edit-header-style-options"]'
);

if (headerStyles !== null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check if (headerStyles !== null) should instead check for headerStyles.length > 0. It’s better to check if any elements are found before adding listeners.

}

function setImageTheme() {
let defaultHeaderStyle = document.querySelector(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a guard clause to prevent this error in case no radio buttons are checked

if (headerStyles !== null) {
headerStyles.forEach((style) => {
style.addEventListener("change", () => {
setImageTheme();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of repeatedly defining an anonymous function for style.addEventListener("change", ...), you can directly pass the reference to the setImageTheme function.


const headerStyles = document.querySelectorAll(
'input[data-drupal-selector^="edit-header-style-options"]'
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The script accesses document multiple times. It’s a good practice to minimize global object access by caching elements that you reuse.

}
}

function setImageTheme() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest moving the setImageTheme() function outside the main attach function for clarity and better separation of logic.

@yeniatencio
Copy link
Contributor Author

@yeniatencio I have suggested some suggestions. Please have a look into the revised version and it should work in the same way -

(function (Drupal) {
  "use strict";

  Drupal.behaviors.heroImageTheme = {
    attach: function (context, settings) {
      const imageTheme = context.querySelector(
        'select[data-drupal-selector^="edit-field-landing-page-hero-theme"]'
      );

      if (imageTheme !== null) {
        setImageTheme();

        const headerStyles = context.querySelectorAll(
          'input[data-drupal-selector^="edit-header-style-options"]'
        );

        if (headerStyles.length > 0) {
          headerStyles.forEach((style) => {
            style.addEventListener("change", setImageTheme);
          });
        }
      }

      function setImageTheme() {
        const selectedHeaderStyle = document.querySelector(
          'input[data-drupal-selector^="edit-header-style-options"]:checked'
        );

        if (!selectedHeaderStyle) {
          return; // Exit if no style option is selected.
        }

        let defaultHeaderStyle = selectedHeaderStyle.value;
        imageTheme.value = defaultHeaderStyle === "fullwidth" ? "dark" : "light";
      }
    },
  };
})(Drupal);

Hi @MdNadimHossain , Thanks for the suggestions. I have updated my pr as you requested.

@yeniatencio
Copy link
Contributor Author

Hey @MdNadimHossain , I have updated my pr because there is a bug present when using states. In our case it happens when a hero image is added, then the hero image theme dropdown was being disabled.
For reference you can see here https://www.drupal.org/project/drupal/issues/1091852.
Now it works as requested:

Screenshot 2024-10-28 at 4 22 54 pm

@vincent-gao
Copy link
Contributor

hey @yeniatencio
I believe this can be achieved using #ajax without adding custom JavaScript code. As @MdNadimHossain pointed out, we should avoid managing additional JS solely for visual enhancements. You can also implement a submit handler to validate the form if needed.

@vincent-gao
Copy link
Contributor

for JS itself.

  1. Limit the DOM operations within the context. https://github.com/dpc-sdp/tide_core/pull/516/files#diff-74b152131cfda4b1eb7d53850fae87f6de6abcbc57cd5143247a4b8480acc46aR25
  2. Use the once method to avoid repeatedly binding event listeners when behaviors are executed multiple times

Copy link
Contributor

@vincent-gao vincent-gao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey @yeniatencio
Please review my comments and provide the test code for this new feature

@vincent-gao
Copy link
Contributor

hey @yeniatencio , What I’ve changed is just a preliminary idea for your further work, and it’s not ready for a code review yet. I think there are still some places that need adjustments.

  1. Remove the JS and its library.
  2. Rename the function at https://github.com/dpc-sdp/tide_core/pull/516/files#diff-9e825e979aa05cc956b3d75ad6d8700172fa678536966ccb8d1c9ef1a96b1ecfR389, as the current name doesn't make sense.
  3. With this approach, you may need to set a default value for the field, which will likely require an update hook.
  4. Update the tests to cover the following scenarios:
    1. Selecting a value from the field
    2. Get excepted Hero image theme type
    3. Save the node
    4. Ensuring the Hero image theme displays the expected value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants